<?php
/**
 * Created by PhpStorm.
 * User: zhang_000
 * Date: 2015/9/8
 * Time: 16:56
 */
/**
 * @RoutePrefix("/manager")
 */

class ManagerController extends ControllerBase{


    public function initialize()
    {
        $this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_NO_RENDER);
    }

    /**
     * @desc 1.2    发送验证码接口
     * @Route("/userlist", methods={"GET"}, name="userlist")
     */
    public function userlistAction(){
       if($this->request->isGet()){
            $users = Users::find();
           $currentPage = 0;
           $data = array();
           $paginator = new \Phalcon\Paginator\Adapter\Model(
               array(
                   "data" => $users,
                   "limit"=> 10,
                   "page" => $currentPage
               )
           );
//           $pageItem=$paginator->getPaginate();
            foreach($users as $user){
                $data[] = array(
                    'id' => $user->id,
                    'username' => $user->username,
                    'password' => $user->password,
                    'name' => $user->name,
                    'sex' => $user->sex,
                    'gcode' => $user->gcode,
                    'department' => $user->phone,
                    'phone' => $user->id,
                    'tel' => $user->tel,
                );
            }
           echo json_encode($data);
       }
    }

    /**
     * @desc 1.2    发送验证码接口
     * @Route("/useradd", methods={"POST"}, name="useradd")
     */
    public function useraddAction(){

        if($this->request->isPost()){
            $user = new Users();
            $user->setUsername($this->request->getPost("username"));
            $user->setPassword('000000');
            $user->setName($this->request->getPost("name"));
            $user->setSex($this->request->getPost("sex"));
            $user->setGcode($this->request->getPost("gcode"));
            $user->setDepartment($this->request->getPost("department"));
            $user->setPhone($this->request->getPost("phone"));
            $user->setTel($this->request->getPost("tel"));

            if($user->save()){
                echo json_encode($user);
            }
        }
    }

    /**
     * @desc 1.2    发送验证码接口
     * @Route("/userdel", methods={"POST"}, name="userdel")
     */
    public function userdelAction(){

        if($this->request->isPost()){

            $user = Users::findFirstById($this->request->getPost("id"));
            if($user){
                $user->delete();
            }
        }
    }

    /**
     * @desc 1.2    发送验证码接口
     * @Route("/useredit", methods={"POST"}, name="useredit")
     */
    public function usereditAction(){

        if($this->request->isPost()){
            $user = Users::findFirstById($this->request->getPost("id"));
            if($user){
                $user->setUsername($this->request->getPost("username"));
                $user->setPassword('000000');
                $user->setName($this->request->getPost("name"));
                $user->setSex($this->request->getPost("sex"));
                $user->setGcode($this->request->getPost("gcode"));
                $user->setDepartment($this->request->getPost("department"));
                $user->setPhone($this->request->getPost("phone"));
                $user->setTel($this->request->getPost("tel"));
            }
            if($user->save()){
                echo json_encode($user);
            }
        }
    }

    /**
     * @desc 1.2    发送验证码接口
     * @Route("/getimg", methods={"GET"}, name="getimg")
     */
    public function getimgAction(){

        if($this->request->isGet()){
            $data = $this->parseHref("http://www.niaola.com/");
            echo json_encode($data);
        }
    }
    /**
     * @desc 1.2    发送验证码接口
     * @Route("/splid", methods={"GET"}, name="splid")
     */
    public function splidAction(){
        $ret =[];
        $url = "http://www.niaola.com/desk/";
        $random= mt_rand(30,177677);
        if($random<177677-300){
            for($i=1;$i<2;$i++){
                $data = $this->getUrlImgs($url.($random+$i));
                $ret = array_merge_recursive($ret,$data);
            }
        }
        echo json_encode($ret);
    }

    /**
     * @desc 1.2    发送验证码接口
     * @Route("/img", methods={"GET"}, name="getimg")
     */
    public function imgAction(){
        if($this->request->isGet()){
            $img = ImgUrl::find(array(
                "limit" => 20
            ));
            echo json_encode($img->toArray());
        }
    }

    protected function parseHref($url){
        $ret =[];
        $url = "http://www.niaola.com/desk/";
        $random= mt_rand(30,177677);
        if($random<177677-300){
            for($i=30;$i<177677;$i++){
                $this->getUrlImgs($url.$random.$i);
            }
        }

        return $ret;
    }


    protected function getUrlImgs($url){
        $ret = [];
        $html=$this->phtml;
        try{
            $html->load_file($url);
            $imgs = $html->find("img");
            foreach($imgs as $img){
                $imgurl = new ImgUrl();
                if(mb_strpos($img->src,'http')>-1){
                    $imgurl->setImgUrl(strstr($img->src,'http'));
                    $imgurl->setImgAlt($img->alt);
                }else{
                    continue;
                }

                if(ImgUrl::findFirstByImgUrl($img->src)){
                    $ret[]=$imgurl;
                }else{
                    if($imgurl->save()){
                        $ret[]=$imgurl;
                    }
                }
            }
        }catch (Exception $e){
            return $ret;
        }

        return $ret;
    }

}